const Koa = require('koa');
const bodyParser = require('koa-bodyparser');
const Router = require('koa-router');
const setRoutes = require('./routes/index');
const db = require('./db/index');

const app = new Koa();
const router = new Router();

// Middleware
app.use(bodyParser());

// Set up routes
setRoutes(router);
app.use(router.routes()).use(router.allowedMethods());

// Database connection
db.connect()
  .then(() => {
    console.log('Database connected successfully');
  })
  .catch(err => {
    console.error('Database connection error:', err);
  });

// Start the server
const PORT = process.env.PORT || 18888;
app.listen(PORT, '0.0.0.0', () => {
  console.log(`Server is running on http://0.0.0.0:${PORT}`);
});